/**
 * @file onvif_test.cpp
 * @brief ONVIF模块测试程序
 * 
 * 本文件包含ONVIF模块的基本功能测试
 */

#include "../include/onvif_server.hpp"
#include <iostream>
#include <thread>
#include <chrono>

using namespace El::Onvif;

void testBasicFunctionality() {
    std::cout << "=== ONVIF 基本功能测试 ===" << std::endl;
    
    try {
        // 创建ONVIF服务器
        OnvifServer server(8080);
        
        // 测试设备信息设置
        DeviceInformation deviceInfo;
        deviceInfo.manufacturer = "Test Manufacturer";
        deviceInfo.model = "Test Camera Model";
        deviceInfo.firmwareVersion = "1.0.0";
        deviceInfo.serialNumber = "TEST123456";
        deviceInfo.hardwareId = "HW-TEST-001";
        
        server.SetDeviceInformation(deviceInfo);
        
        // 验证设备信息获取
        auto retrievedInfo = server.GetDeviceInformation();
        if (retrievedInfo.manufacturer == deviceInfo.manufacturer) {
            std::cout << "✓ 设备信息设置/获取测试通过" << std::endl;
        } else {
            std::cout << "✗ 设备信息设置/获取测试失败" << std::endl;
        }
        
        // 测试网络配置
        NetworkInterface networkConfig;
        networkConfig.interfaceToken = "eth0";
        networkConfig.enabled = true;
        networkConfig.info.name = "eth0";
        networkConfig.info.hwAddress = "00:11:22:33:44:55";
        networkConfig.info.mtu = 1500;
        networkConfig.ipv4.enabled = true;
        networkConfig.ipv4.config.manual.address = "192.168.1.100";
        networkConfig.ipv4.config.manual.prefixLength = 24;
        
        server.SetNetworkConfiguration(networkConfig);
        
        // 验证网络配置获取
        auto retrievedNetwork = server.GetNetworkConfiguration();
        if (retrievedNetwork.info.hwAddress == networkConfig.info.hwAddress) {
            std::cout << "✓ 网络配置设置/获取测试通过" << std::endl;
        } else {
            std::cout << "✗ 网络配置设置/获取测试失败" << std::endl;
        }
        
        // 测试用户管理
        User testUser;
        testUser.username = "testuser";
        testUser.password = "testpass";
        testUser.userLevel = UserLevel::User;
        
        server.AddUser(testUser);
        
        auto users = server.GetUsers();
        bool userFound = false;
        for (const auto& user : users) {
            if (user.username == "testuser") {
                userFound = true;
                break;
            }
        }
        
        if (userFound) {
            std::cout << "✓ 用户管理测试通过" << std::endl;
        } else {
            std::cout << "✗ 用户管理测试失败" << std::endl;
        }
        
        // 测试媒体配置文件
        MediaProfile profile;
        profile.token = "TestProfile";
        profile.name = "Test Media Profile";
        profile.fixed = false;
        profile.videoSourceConfiguration.sourceToken = "VideoSource_Test";
        profile.videoSourceConfiguration.name = "Test Video Source";
        profile.videoSourceConfiguration.useCount = 1;
        profile.videoSourceConfiguration.bounds.x = 0;
        profile.videoSourceConfiguration.bounds.y = 0;
        profile.videoSourceConfiguration.bounds.width = 1920;
        profile.videoSourceConfiguration.bounds.height = 1080;
        
        server.AddMediaProfile(profile);
        
        auto profiles = server.GetMediaProfiles();
        bool profileFound = false;
        for (const auto& p : profiles) {
            if (p.token == "TestProfile") {
                profileFound = true;
                break;
            }
        }
        
        if (profileFound) {
            std::cout << "✓ 媒体配置文件管理测试通过" << std::endl;
        } else {
            std::cout << "✗ 媒体配置文件管理测试失败" << std::endl;
        }
        
        // 测试服务能力查询
        auto capabilities = server.GetServiceCapabilities();
        if (!capabilities.device.xAddr.empty()) {
            std::cout << "✓ 服务能力查询测试通过" << std::endl;
        } else {
            std::cout << "✗ 服务能力查询测试失败" << std::endl;
        }
        
        std::cout << "=== 基本功能测试完成 ===" << std::endl;
        
    } catch (const std::exception& e) {
        std::cout << "✗ 测试过程中发生异常: " << e.what() << std::endl;
    }
}

void testServerStartStop() {
    std::cout << "\n=== ONVIF 服务器启动/停止测试 ===" << std::endl;
    
    try {
        OnvifServer server(8080);
        
        // 测试服务器启动
        bool startResult = server.Start();
        if (startResult) {
            std::cout << "✓ 服务器启动成功" << std::endl;
            
            // 检查运行状态
            if (server.IsRunning()) {
                std::cout << "✓ 服务器运行状态检查通过" << std::endl;
            } else {
                std::cout << "✗ 服务器运行状态检查失败" << std::endl;
            }
            
            // 让服务器运行一小段时间
            std::this_thread::sleep_for(std::chrono::seconds(2));
            
            // 测试服务器停止
            server.Stop();
            std::cout << "✓ 服务器停止成功" << std::endl;
            
            // 检查停止状态
            if (!server.IsRunning()) {
                std::cout << "✓ 服务器停止状态检查通过" << std::endl;
            } else {
                std::cout << "✗ 服务器停止状态检查失败" << std::endl;
            }
            
        } else {
            std::cout << "✗ 服务器启动失败" << std::endl;
        }
        
        std::cout << "=== 服务器启动/停止测试完成 ===" << std::endl;
        
    } catch (const std::exception& e) {
        std::cout << "✗ 服务器测试过程中发生异常: " << e.what() << std::endl;
    }
}

void testWSDLService() {
    std::cout << "\n=== WSDL文档服务测试 ===" << std::endl;
    
    try {
        // 创建WSDL服务
        WSDLService wsdlService("http://192.168.1.100");
        
        // 测试路径识别
        bool isWSDLRequest = wsdlService.IsWSDLRequest("/onvif/device_service?wsdl");
        if (isWSDLRequest) {
            std::cout << "✓ WSDL请求路径识别测试通过" << std::endl;
        } else {
            std::cout << "✗ WSDL请求路径识别测试失败" << std::endl;
        }
        
        // 测试Device WSDL生成
        std::string deviceWSDL = wsdlService.GetWSDLByPath("/onvif/device_service?wsdl");
        if (!deviceWSDL.empty() && deviceWSDL.find("DeviceService") != std::string::npos) {
            std::cout << "✓ Device WSDL生成测试通过" << std::endl;
        } else {
            std::cout << "✗ Device WSDL生成测试失败" << std::endl;
        }
        
        // 测试Media WSDL生成
        std::string mediaWSDL = wsdlService.GetWSDLByPath("/onvif/media_service?wsdl");
        if (!mediaWSDL.empty()) {
            std::cout << "✓ Media WSDL生成测试通过" << std::endl;
        } else {
            std::cout << "✗ Media WSDL生成测试失败" << std::endl;
        }
        
        // 测试PTZ WSDL生成
        std::string ptzWSDL = wsdlService.GetWSDLByPath("/onvif/ptz_service?wsdl");
        if (!ptzWSDL.empty()) {
            std::cout << "✓ PTZ WSDL生成测试通过" << std::endl;
        } else {
            std::cout << "✗ PTZ WSDL生成测试失败" << std::endl;
        }
        
        std::cout << "=== WSDL文档服务测试完成 ===" << std::endl;
        
    } catch (const std::exception& e) {
        std::cout << "✗ WSDL服务测试过程中发生异常: " << e.what() << std::endl;
    }
}

void testSOAPMessageParser() {
    std::cout << "\n=== SOAP消息解析器测试 ===" << std::endl;
    
    try {
        SoapMessageParser parser;
        
        // 测试SOAP响应生成
        std::string responseBody = "<tds:GetDeviceInformationResponse>"
                                  "<tds:Manufacturer>Test</tds:Manufacturer>"
                                  "</tds:GetDeviceInformationResponse>";
        
        std::string soapResponse = parser.GenerateSoapResponse(responseBody, "GetDeviceInformationResponse");
        
        if (!soapResponse.empty() && soapResponse.find("soap:Envelope") != std::string::npos) {
            std::cout << "✓ SOAP响应生成测试通过" << std::endl;
        } else {
            std::cout << "✗ SOAP响应生成测试失败" << std::endl;
        }
        
        // 测试SOAP错误生成
        std::string soapFault = parser.GenerateSoapFault(SoapError::AuthenticationFailed, "Test error");
        
        if (!soapFault.empty() && soapFault.find("soap:Fault") != std::string::npos) {
            std::cout << "✓ SOAP错误生成测试通过" << std::endl;
        } else {
            std::cout << "✗ SOAP错误生成测试失败" << std::endl;
        }
        
        std::cout << "=== SOAP消息解析器测试完成 ===" << std::endl;
        
    } catch (const std::exception& e) {
        std::cout << "✗ SOAP解析器测试过程中发生异常: " << e.what() << std::endl;
    }
}

int main() {
    std::cout << "开始ONVIF模块测试..." << std::endl;
    std::cout << "==========================================" << std::endl;
    
    // 运行各项测试
    testBasicFunctionality();
    testServerStartStop();
    testWSDLService();
    testSOAPMessageParser();
    
    std::cout << "\n==========================================" << std::endl;
    std::cout << "ONVIF模块测试完成！" << std::endl;
    
    return 0;
} 